<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父货位" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父货位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货位编码" prop="locationCode">
        <el-input
          v-model="queryParams.locationCode"
          placeholder="请输入货位编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货位名称" prop="locationName">
        <el-input
          v-model="queryParams.locationName"
          placeholder="请输入货位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="长" prop="locationLength">
        <el-input
          v-model="queryParams.locationLength"
          placeholder="请输入长"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宽" prop="locationWidth">
        <el-input
          v-model="queryParams.locationWidth"
          placeholder="请输入宽"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高" prop="locationHeight">
        <el-input
          v-model="queryParams.locationHeight"
          placeholder="请输入高"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="承重" prop="locationWeight">
        <el-input
          v-model="queryParams.locationWeight"
          placeholder="请输入承重"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入机构"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构" prop="orgId">
        <el-input
          v-model="queryParams.orgId"
          placeholder="请输入机构"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:whLocation:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="whLocationList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="仓库" prop="warehouseId" />
      <el-table-column label="父货位" align="center" prop="parentId" />
      <el-table-column label="货位编码" align="center" prop="locationCode" />
      <el-table-column label="货位名称" align="center" prop="locationName" />
      <el-table-column label="长" align="center" prop="locationLength" />
      <el-table-column label="宽" align="center" prop="locationWidth" />
      <el-table-column label="高" align="center" prop="locationHeight" />
      <el-table-column label="承重" align="center" prop="locationWeight" />
      <el-table-column label="机构" align="center" prop="deptId" />
      <el-table-column label="机构" align="center" prop="orgId" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:whLocation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['base:whLocation:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:whLocation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改货位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width=600px append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
           <el-col :span=12>
            <el-form-item label="仓库" prop="warehouseId">
              <el-input v-model="form.warehouseId" placeholder="请输入仓库" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="父货位" prop="parentId">
              <treeselect v-model="form.parentId" :options="whLocationOptions" :normalizer="normalizer" placeholder="请选择父货位" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="货位编码" prop="locationCode">
              <el-input v-model="form.locationCode" placeholder="请输入货位编码" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="货位名称" prop="locationName">
              <el-input v-model="form.locationName" placeholder="请输入货位名称" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="长" prop="locationLength">
              <el-input v-model="form.locationLength" placeholder="请输入长" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="宽" prop="locationWidth">
              <el-input v-model="form.locationWidth" placeholder="请输入宽" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="高" prop="locationHeight">
              <el-input v-model="form.locationHeight" placeholder="请输入高" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="承重" prop="locationWeight">
              <el-input v-model="form.locationWeight" placeholder="请输入承重" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="机构" prop="deptId">
              <el-input v-model="form.deptId" placeholder="请输入机构" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="机构" prop="orgId">
              <el-input v-model="form.orgId" placeholder="请输入机构" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
:label="dict.value"                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
           </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm(false)">保 存</el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm(true)">保存并新增</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWhLocation, getWhLocation, delWhLocation, addWhLocation, updateWhLocation } from "@/api/base/whLocation";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "WhLocation",
  dicts: ['sys_normal_disable'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 货位表格数据
      whLocationList: [],
      // 货位树选项
      whLocationOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        warehouseId: null,
        parentId: null,
        locationCode: null,
        locationName: null,
        locationLength: null,
        locationWidth: null,
        locationHeight: null,
        locationWeight: null,
        deptId: null,
        orgId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        warehouseId: [
          { required: true, message: "仓库不能为空", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "父货位不能为空", trigger: "blur" }
        ],
        locationCode: [
          { required: true, message: "货位编码不能为空", trigger: "blur" }
        ],
        locationName: [
          { required: true, message: "货位名称不能为空", trigger: "blur" }
        ],
        locationLength: [
          { required: true, message: "长不能为空", trigger: "blur" }
        ],
        locationWidth: [
          { required: true, message: "宽不能为空", trigger: "blur" }
        ],
        locationHeight: [
          { required: true, message: "高不能为空", trigger: "blur" }
        ],
        locationWeight: [
          { required: true, message: "承重不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "机构不能为空", trigger: "blur" }
        ],
        orgId: [
          { required: true, message: "机构不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询货位列表 */
    getList() {
      this.loading = true;
      listWhLocation(this.queryParams).then(response => {
        this.whLocationList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换货位数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.locationName,
        children: node.children
      };
    },
	/** 查询货位下拉树结构 */
    getTreeselect() {
      listWhLocation().then(response => {
        this.whLocationOptions = [];
        const data = { id: 0, locationName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.whLocationOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 保存并新增操作 */
    handleSaveAdd() {
      const parentId = this.form.parentId;
      this.reset();
      this.getTreeselect();
      if (parentId != null) {
          this.form.parentId = parentId;
      } else {
          this.form.parentId = 0;
      }
      this.title = "添加货位";
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehouseId: null,
        parentId: null,
        locationCode: null,
        locationName: null,
        locationLength: null,
        locationWidth: null,
        locationHeight: null,
        locationWeight: null,
        deptId: null,
        orgId: null,
        status: "0",

        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加货位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
	  this.loading = true;
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getWhLocation(row.id).then(response => {
	    this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改货位";
      });
    },
    /** 提交按钮 */
    submitForm(isAdd) {
      this.$refs["form"].validate(valid => {
        if (valid) {
		  this.buttonLoading = true;
          if (this.form.id != null) {
            updateWhLocation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              if (isAdd){
                  this.handleSaveAdd();
              }else {
                  this.open = false;
              }
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addWhLocation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              if (isAdd){
                  this.handleSaveAdd();
              }else {
                  this.open = false;
              }
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除货位编号为"' + row.id + '"的数据项？').then(() => {
        this.loading = true;
        return delWhLocation(row.id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    }
  }
};
</script>
